import numpy as np
import scipy.special as sp
import scipy.integrate as ig
import copy
from Model import Model
from Model import Actor
from Model import Evidence
from Model import TrackSegment
from Model import Node
from Model import Distributions


class Run:

    def __init__(self):
        self.model = Model()
        self.model.name = "Run"
        self.model.subscriptions = [""]
        self.model.topNode = "runmain"
        self.model.independent = True

        act1 = Actor()
        act1.name = "dummy1"
        act1.atype = Actor.NULL

        tempNode = Node()
        tempNode.getID()
        tempNode.name = "runmain"
        tempNode.subjects = [act1]
        tempNode.objects = []
        tempNode.evvecs = []
        tempNode.bound = False

        self.model.topNode = tempNode.name
        self.model.nodes.append(copy.deepcopy(tempNode))

        tempNode.name = "runsub"
        tempEv = TrackSegment()
        tempEv.actor = act1
        tempEv.segtype = Evidence.MOVE_FAST
        tempEv.willBind = True
        tempEv.endsNode = True
        tempEv.bound = False

        tempNode.evvecs = [copy.deepcopy(tempEv)]

        def runJoint(evvecs):
            track = evvecs[0]
            speed = track.calculate_speed()
            print "calculated speed: %s" % speed
            speedWeight = Run.speedDensity(speed)
            print "weight: %s" % speedWeight
            time = (track.endtime - track.starttime)/Model.FR/60.
            print "time: %s" % time
            timeWeight = Run.timeDensity(time)
            print "time weight: %s" % timeWeight
            totalWeight = speedWeight + timeWeight
            return totalWeight, .5

        tempNode.lfunction = runJoint

        self.model.nodes.append(copy.deepcopy(tempNode))

    @staticmethod
    def speedDensity(speed):
        """saying average walking speed is ~1.4 m/s"""
        weight = Distributions.PDFs.gamma(speed, 14., 1/1.25)
        return weight

    @staticmethod
    def timeDensity(time):
        #loitering time is in minutes
        weight = Distributions.PDFs.gamma(time, 3.5, .75)
        return weight
